<?php

namespace app\modules\merchant\services;

use app\common\services\BaseService;
use app\models\GoodsAttribute;
use app\modules\merchant\forms\goods\GoodsAttributeForm;

class GoodsAttributeService extends BaseService{

    /**
     * 搜索
     * @param $goods_id
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function search($goods_id){
        return GoodsAttribute::find()
            ->asArray(true)
            ->andWhere(['goods_id'=>$goods_id])
            ->all();
    }

    /**
     * 创建
     * @param GoodsAttributeForm $form
     * @return bool
     * @throws \Throwable
     */
    public static function create(GoodsAttributeForm $form){
        if(!$form->validate()){
            self::setError($form->getFirstFieldError());
            return false;
        }
        $attr = new GoodsAttribute();
        $attr->setAttributes($form->getAttributes(),false);
        return $attr->insert() !== false;
    }
}